package com.liuchuanzheng.lcz_wanandroid.base

import android.os.Bundle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType

/**
 * @author 刘传政
 * @date 2021/9/14 0014 16:20
 * QQ:1052374416
 * 电话:18501231486
 * 作用:
 * 注意事项:
 */
abstract class BaseVMActivity<VM:ViewModel> :BaseActivity(){
    lateinit var mViewModel:VM
    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        initViewModel()
        observeViewModel()
    }



    private fun initViewModel() {
        mViewModel = ViewModelProvider(this,ViewModelProvider.NewInstanceFactory()).get(provideVMClass())
    }

    /**
     * 进行观察订阅
     */
    abstract fun observeViewModel()
    /**
     * 获取类的第一个泛型Class
     * @return Class<VM>?
     */
    private fun provideVMClass(): Class<VM> {
        return (javaClass.genericSuperclass as ParameterizedType).getActualTypeArguments()[0] as Class<VM>
    }
}